﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace CControl
{
    public class Cls_Cliente
    {
        public int id_cliente { get; set; }
        public string nombre_cliente { get; set; }
        public string apellido_cliente { get; set; }
        public string dni_cliente { get; set; }
        public string telefono_cliente { get; set; }
        public string direccion_cliente { get; set; }
        public string referencia_cliente { get; set; }


        public static bool mtd_ValidarIDCliente(string _dni)
        {
            try
            {

                //oConexion.ConnectionString = _ConnectionString;
                //oConexion.Open();
                Cls_Conexion Objconexion = new Cls_Conexion();
                SqlCommand Objcomando = new SqlCommand();
                SqlDataAdapter ObjDataAdapter = new SqlDataAdapter();
                DataTable ObjDataTable = new DataTable();
                //Cls_Cliente _Cliente = new Cls_Cliente();

                Objcomando.CommandType = CommandType.StoredProcedure;
                Objcomando.CommandText = "USP_S_BUSCAR_DNICLIENTE";

                Objcomando.Parameters.Add(new SqlParameter("@DNI", SqlDbType.Char));
                Objcomando.Parameters["@DNI"].Value = _dni;

                Objcomando.Connection = Objconexion.Conectar();
                Objcomando.ExecuteNonQuery();

                ObjDataAdapter.SelectCommand = Objcomando;
                ObjDataAdapter.Fill(ObjDataTable);

                Objconexion = null;
                ObjDataAdapter = null;

                if (ObjDataTable.Rows.Count > 0)
                {
                    DataRow row = ObjDataTable.Rows[0];
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                //throw ex;
                return false;
            }
            finally
            {
                Cls_Conexion desconectar = new Cls_Conexion();
                desconectar.Desconectar();
            }
        }

        public static int mtd_BuscarIDCliente(string _dni)
        {
            try
            {

                //oConexion.ConnectionString = _ConnectionString;
                //oConexion.Open();
                Cls_Conexion Objconexion = new Cls_Conexion();
                SqlCommand Objcomando = new SqlCommand();
                SqlDataAdapter ObjDataAdapter = new SqlDataAdapter();
                DataTable ObjDataTable = new DataTable();
                Cls_Cliente _Cliente = new Cls_Cliente();

                Objcomando.CommandType = CommandType.StoredProcedure;
                Objcomando.CommandText = "USP_S_BUSCAR_DNICLIENTE";

                Objcomando.Parameters.Add(new SqlParameter("@DNI", SqlDbType.Char));
                Objcomando.Parameters["@DNI"].Value = _dni;

                Objcomando.Connection = Objconexion.Conectar();
                Objcomando.ExecuteNonQuery();

                ObjDataAdapter.SelectCommand = Objcomando;
                ObjDataAdapter.Fill(ObjDataTable);

                Objconexion = null;
                ObjDataAdapter = null;

                if (ObjDataTable.Rows.Count > 0)
                {
                    DataRow row = ObjDataTable.Rows[0];
                    _Cliente.id_cliente = Convert.ToInt16(row[0]);
                    _Cliente.nombre_cliente = Convert.ToString(row[1]);
                    _Cliente.apellido_cliente = Convert.ToString(row[2]);
                    _Cliente.dni_cliente = Convert.ToString(row[3]);
                    _Cliente.telefono_cliente = Convert.ToString(row[4]);
                    _Cliente.direccion_cliente = Convert.ToString(row[5]);
                    _Cliente.referencia_cliente = Convert.ToString(row[6]);
                }

                return _Cliente.id_cliente;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Cls_Conexion desconectar = new Cls_Conexion();
                desconectar.Desconectar();
            }
        }


        public bool mtd_AgregarCliente()
        {
            try
            {

                //oConexion.ConnectionString = _ConnectionString;
                //oConexion.Open();
                Cls_Conexion Objconexion = new Cls_Conexion();
                SqlCommand Objcomando = new SqlCommand();
                SqlDataAdapter ObjDataAdapter = new SqlDataAdapter();
                DataTable ObjDataTable = new DataTable();
                Cls_Cliente _Cliente = new Cls_Cliente();

                Objcomando.CommandType = CommandType.StoredProcedure;
                Objcomando.CommandText = "USP_I_INSERTAR_CLIENTE";

                //@CLInombre				
                //@CLIapellido			
                //@CLIdni					
                //@CLItelefono			
                //@CLIdireccion			
                //@CLIdireccion_referencia

                Objcomando.Parameters.Add(new SqlParameter("@CLInombre", SqlDbType.VarChar));
                Objcomando.Parameters.Add(new SqlParameter("@CLIapellido", SqlDbType.VarChar));
                Objcomando.Parameters.Add(new SqlParameter("@CLIdni", SqlDbType.Char));
                Objcomando.Parameters.Add(new SqlParameter("@CLItelefono", SqlDbType.VarChar));
                Objcomando.Parameters.Add(new SqlParameter("@CLIdireccion", SqlDbType.VarChar));
                Objcomando.Parameters.Add(new SqlParameter("@CLIdireccion_referencia", SqlDbType.VarChar));

                Objcomando.Parameters["@CLInombre"].Value = this.nombre_cliente;
                Objcomando.Parameters["@CLIapellido"].Value = this.apellido_cliente;
                Objcomando.Parameters["@CLIdni"].Value = this.dni_cliente;
                Objcomando.Parameters["@CLItelefono"].Value = this.telefono_cliente;
                Objcomando.Parameters["@CLIdireccion"].Value = this.direccion_cliente;
                Objcomando.Parameters["@CLIdireccion_referencia"].Value = this.referencia_cliente;

                Objcomando.Connection = Objconexion.Conectar();
                Objcomando.ExecuteNonQuery();

                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Cls_Conexion desconectar = new Cls_Conexion();
                desconectar.Desconectar();
            }
        }

        public static Cls_Cliente mtd_BuscarCliente(string _dni)
        {
            try
            {

                //oConexion.ConnectionString = _ConnectionString;
                //oConexion.Open();
                Cls_Conexion Objconexion = new Cls_Conexion();
                SqlCommand Objcomando = new SqlCommand();
                SqlDataAdapter ObjDataAdapter = new SqlDataAdapter();
                DataTable ObjDataTable = new DataTable();
                Cls_Cliente _Cliente = new Cls_Cliente();

                Objcomando.CommandType = CommandType.StoredProcedure;
                Objcomando.CommandText = "USP_S_BUSCAR_DNICLIENTE";

                Objcomando.Parameters.Add(new SqlParameter("@DNI", SqlDbType.Char));         
                Objcomando.Parameters["@DNI"].Value = _dni;

                Objcomando.Connection = Objconexion.Conectar();
                Objcomando.ExecuteNonQuery();

                ObjDataAdapter.SelectCommand = Objcomando;
                ObjDataAdapter.Fill(ObjDataTable);

                Objconexion = null;
                ObjDataAdapter = null;

                if (ObjDataTable.Rows.Count > 0)
                {
                    DataRow row = ObjDataTable.Rows[0];
                    _Cliente.id_cliente = Convert.ToInt16(row[0]);
                    _Cliente.nombre_cliente = Convert.ToString(row[1]);
                    _Cliente.apellido_cliente = Convert.ToString(row[2]);
                    _Cliente.dni_cliente = Convert.ToString(row[3]);
                    _Cliente.telefono_cliente = Convert.ToString(row[4]);
                    _Cliente.direccion_cliente = Convert.ToString(row[5]);
                    _Cliente.referencia_cliente = Convert.ToString(row[6]);
                }

                return _Cliente;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Cls_Conexion desconectar = new Cls_Conexion();
                desconectar.Desconectar();
            }
        }
    }
}
